#include "circuitelement.h"
#include "appsettings.h"

#include <QDir>
#include <QDebug>

//# Конструкторы, деструкторы и инициализация

CircuitElement::CircuitElement(){ }

CircuitElement::CircuitElement(uint pins_count)
{
    for (uint i = 1; i <= pins_count; i++ )
        pins.append(this->createSpecialPIN());
}

CircuitElement::~CircuitElement()
{
    qDeleteAll(this->pins);
}

//# Методы

void CircuitElement::reset()
{
    for (int i = 0; i < this->pins.count(); i++)
        this->pins[i]->reset();
}

void CircuitElement::disconnectAll()
{
    for (int i = 0; i < this->pins.count(); i++)
        this->pins[i]->disconnectPin();
}

Pin* CircuitElement::createSpecialPIN()
{
    Pin *a = new Pin(this);
    a->name = QString::number(this->pins.count() + 1);
    return a;
}

Pin* CircuitElement::getPINByName(const QString& name)
{
    int i = 0;
    int count = pins.count();
    while (( i < count) && (pins[i]->name != name))
        i++;
    if (i < count)
        return pins[i];
    else
        return NULL;
}

//Сводная информация по пинам устройства
QString CircuitElement::getInfo()
{
    int pinsCount = pins.count();
    QString intend = "  ";
    QString endline = "\n";
    QString res = metaObject()->className() + QString(":") + name +
                  endline +
                  "PINs: " + QString::number(pinsCount) + endline;

    for (int i = 0; i < pinsCount; i++)
    {
        res += intend + "[" + QString::number(i) + "] : " +
               pins.at(i)->typeToString() + " " +
               pins.at(i)->name + endline;
    }
    return res;
}
